package com.able.wanandroid.http;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;

import com.able.wanandroid.bean.ArticleBean;
import com.able.wanandroid.bean.BannerBean;
import com.able.wanandroid.bean.BaseResult;
import com.able.wanandroid.bean.HotKeyBean;
import com.able.wanandroid.bean.Resource;
import com.able.wanandroid.bean.TreeBean;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author Able
 * @date 2018/3/20
 */
public class HttpClient {
    public static final HttpClient instance = new HttpClient();

    public HttpClient() {
    }

    public static HttpClient getInstance() {
        return instance;
    }

    private RetrofitApi api = RetrofitFactory.getInstance().create(RetrofitApi.class);


    /**
     * 查询文章列表
     */
    public LiveData<ArticleBean> queryArticle() {
        final MutableLiveData<ArticleBean> articleLiveData = new MutableLiveData<>();
        api.queryArticle().enqueue(new Callback<ArticleBean>() {
            @Override
            public void onResponse(Call<ArticleBean> call, Response<ArticleBean> response) {
                articleLiveData.setValue(response.body());
            }

            @Override
            public void onFailure(Call<ArticleBean> call, Throwable t) {

            }
        });
        return articleLiveData;
    }

    public LiveData<ArticleBean> loadMoreArticle(int pageNo) {
        final MutableLiveData<ArticleBean> articleLiveData = new MutableLiveData<>();
        api.loadMoreArticle(pageNo).enqueue(new Callback<ArticleBean>() {
            @Override
            public void onResponse(Call<ArticleBean> call, Response<ArticleBean> response) {
                articleLiveData.setValue(response.body());
            }

            @Override
            public void onFailure(Call<ArticleBean> call, Throwable t) {

            }
        });
        return articleLiveData;
    }

    /**
     * 获取首页Banner
     */
    public LiveData<BannerBean> getBanner() {
        final MutableLiveData<BannerBean> bannerLiveData = new MutableLiveData<>();
        api.getBanner().enqueue(new Callback<BannerBean>() {
            @Override
            public void onResponse(Call<BannerBean> call, Response<BannerBean> response) {
                bannerLiveData.setValue(response.body());
            }

            @Override
            public void onFailure(Call<BannerBean> call, Throwable t) {

            }
        });
        return bannerLiveData;
    }

    /**
     * 获取热词
     */
    public LiveData<Resource<List<HotKeyBean>>> getHotKey() {
        final MutableLiveData<Resource<List<HotKeyBean>>> liveData = new MutableLiveData<>();
        liveData.setValue(Resource.<List<HotKeyBean>>loading());
        api.getHotKey().enqueue(new ApiCallback<BaseResult<List<HotKeyBean>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<HotKeyBean>>> call, BaseResult<List<HotKeyBean>> result) {
                liveData.setValue(Resource.response(result));
            }

            @Override
            public void onError(Call<BaseResult<List<HotKeyBean>>> call, Throwable t) {
                liveData.setValue(Resource.<List<HotKeyBean>>error(t));
            }
        });
        return liveData;
    }

    public LiveData<Resource<List<TreeBean>>> getTreeData() {
        final MutableLiveData<Resource<List<TreeBean>>> liveData = new MutableLiveData<>();
        api.getTreeData().enqueue(new ApiCallback<BaseResult<List<TreeBean>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<TreeBean>>> call, BaseResult<List<TreeBean>> result) {
                liveData.setValue(Resource.response(result));
            }

            @Override
            public void onError(Call<BaseResult<List<TreeBean>>> call, Throwable t) {
                liveData.setValue(Resource.<List<TreeBean>>error(t));
            }
        });
        return liveData;
    }


    public LiveData<ArticleBean> getArticleTree(int pageNo, int cid) {
        final MutableLiveData<ArticleBean> articleLiveData = new MutableLiveData<>();
        api.getArticleTree(pageNo, cid).enqueue(new Callback<ArticleBean>() {
            @Override
            public void onResponse(Call<ArticleBean> call, Response<ArticleBean> response) {
                articleLiveData.setValue(response.body());
            }

            @Override
            public void onFailure(Call<ArticleBean> call, Throwable t) {

            }
        });
        return articleLiveData;
    }
}
